package com.yjs.app.trade.web.controller.interceptor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yjs.app.trade.core.util.StringUtils;

import net.sf.json.JSONArray;

public class AuthenticationUtil {

	private static String buyerAutPermissionId = "PERMISSION_12000001";
	
	private static String sellerAutPermissionId = "PERMISSION_12100001";
	
	private static String dispatchAutPermissionId = "PERMISSION_12200001";
	
	
	public static Map<String, Integer> getUserAuts (List<String> types, JSONArray jsonArray) {
		Map<String, Integer> map = new HashMap<>();
		List<String> autPermissionIds = new ArrayList<>();
		for (String str : types) {
			if (StringUtils.equals("BUYER", str)) {
				autPermissionIds.add(buyerAutPermissionId);
				map.put("BUYER", 2);
			} else if (StringUtils.equals("SELLER", str)) {
				autPermissionIds.add(sellerAutPermissionId);
				map.put("SELLER", 2);
			} else if (StringUtils.equals("DISPATCH", str)) {
				autPermissionIds.add(dispatchAutPermissionId);
				map.put("DISPATCH", 2);
			} else if (StringUtils.equals("SUPERVISION", str)) {
				map.put("SUPERVISION", 1);
			} else if (StringUtils.equals("MANAGER", str)) {
				map.put("MANAGER", 1);
			}
			for (Object object : jsonArray) {
				for (String autPermissionId : autPermissionIds) {
					if (StringUtils.equals(autPermissionId, object.toString())) {
						String memberType = getMemberTypeByAutPermissionId(autPermissionId);
						if (map.get(memberType) != null) {
							map.put(memberType, 1);
						}
					}
				}
			}
		}
		return map;
	}
	
	public static String getMemberTypeByAutPermissionId (String autPermissionId) {
		if (StringUtils.equals(buyerAutPermissionId, autPermissionId)) {
			return "BUYER";
		} else if (StringUtils.equals(sellerAutPermissionId, autPermissionId)) {
			return "SELLER";
		} else if (StringUtils.equals(dispatchAutPermissionId, autPermissionId)) {
			return "DISPATCH";
		}
		return "";
	}
}
